package com.itheima.web;

import com.itheima.bean.ShoppingCart;
import com.itheima.common.R;
import com.itheima.sevrice.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/shoppingCart")
@RestController
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 加入购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart){
        ShoppingCart cart= shoppingCartService.add(shoppingCart);
       return R.success(cart);
    }

    /**
     * 查看购物车
     * @param
     * @return
     */
    @GetMapping("/list")
    public R findShoppingCart(){
        List<ShoppingCart> cartList = shoppingCartService.findShoppingCart();
        return R.success(cartList);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R delete(){
        int row = shoppingCartService.delete();
        if (row>0){
            return R.success("清空购物车成功！");
        }
        return R.error("清空购物车失败！");
    }

    /**
     * 购物车减少一件菜品|套餐
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R sub(@RequestBody ShoppingCart shoppingCart){
        ShoppingCart shoppingCart1 = shoppingCartService.sub(shoppingCart);
        return R.success(shoppingCart1);
    }


}
